﻿using SeeSharpTools.JY.DSP.Utility.Filter1D;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace SeeSharpTools.Demo
{
    public partial class FormIIR : UIPage
    {
        public FormIIR()
        {
            InitializeComponent();

            uiComboBox_DesignMethod.Items.AddRange(Enum.GetNames(typeof(IIRDesignMethod)));
            uiComboBox_DesignMethod.SelectedIndex = 0;
            uiComboBox_BandType.Items.AddRange(Enum.GetNames(typeof(IIRBandType)));
            uiComboBox_BandType.SelectedIndex = 2;
        }

        private void uiButton_Design_Click(object sender, EventArgs e)
        {
            try
            {
                easyChartX_Mag.Miscellaneous.CheckInfinity = true;
                easyChartX_Phase.Miscellaneous.CheckInfinity = true;
                easyChartX_GrpDelay.Miscellaneous.CheckInfinity = true;
                easyChartX_Impulse.Miscellaneous.CheckInfinity = true;

                double sampleRate = uiDoubleUpDown_SampleRate.Value;
                double[] wn = Array.ConvertAll(uiTextBox_Cutoff.Text.Split(','), Convert.ToDouble);
                var (b, a) = IIRDesign.TransferFunction(uiIntegerUpDown_Order.Value, wn, uiDoubleUpDown_PassRipple.Value,
                    uiDoubleUpDown_StopRipple.Value, (IIRBandType)Enum.Parse(typeof(IIRBandType), uiComboBox_BandType.Text),
                    (IIRDesignMethod)Enum.Parse(typeof(IIRDesignMethod), uiComboBox_DesignMethod.Text), sampleRate);
                var (mag, phase, f) = IIRAnalysis.Bode(b, a, sampleRate);
                easyChartX_Mag.Plot(f, mag);
                easyChartX_Phase.Plot(f, phase);
                var (gd, _) = IIRAnalysis.GroupDelay(b, a, sampleRate);
                easyChartX_GrpDelay.Plot(f, gd);
                var (h, t) = IIRAnalysis.ImpulseResponse(b, a, sampleRate);
                easyChartX_Impulse.Plot(t, h);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void uiComboBox_DesignMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBox_DesignMethod.SelectedIndex == 0 || uiComboBox_DesignMethod.SelectedIndex == 4)
            {
                // 巴特沃斯或者贝塞尔滤波器，没有纹波设置
                uiDoubleUpDown_PassRipple.Enabled = false;
                uiDoubleUpDown_StopRipple.Enabled = false;
            }
            else if (uiComboBox_DesignMethod.SelectedIndex == 1)
            {
                // 切比雪夫I型滤波器，只要设置通带纹波
                uiDoubleUpDown_PassRipple.Enabled = true;
                uiDoubleUpDown_StopRipple.Enabled = false;
            }
            else if (uiComboBox_DesignMethod.SelectedIndex == 2)
            {
                // 切比雪夫II型滤波器，只要设置阻带纹波
                uiDoubleUpDown_PassRipple.Enabled = false;
                uiDoubleUpDown_StopRipple.Enabled = true;
            }
            else if (uiComboBox_DesignMethod.SelectedIndex == 3)
            {
                // 椭圆滤波器，同时设置通带和阻带纹波
                uiDoubleUpDown_PassRipple.Enabled = true;
                uiDoubleUpDown_StopRipple.Enabled = true;
            }
        }
    }
}